using System;
using System.Collections.Generic;
using System.Text;
using CodeBackpack.Test.Http;
using NUnit.Framework;

[TestFixture]
public class TagHandlerTestFixture
{
    private Dictionary<string, Tag> tags;

    [SetUp] public void SetUp() {
        tags = new Dictionary<String, Tag>();
    }

    [Test, ExpectedException(typeof (ArgumentException))]
    public void PostTagInvalidRequestContentType() {
        HttpHandlerTest http = HttpHandlerTest.ImitatePost("tags.ashx", "{}", Encoding.UTF8);
        http.Context.Request.ContentType = "text/xml";
        http.Execute(new TagHandler(tags));
    }

    [Test] public void PostTag() {
        HttpHandlerTest http = HttpHandlerTest.ImitatePost("tags.ashx", 
            "{\"name\":\"foo\",\"description\":\"a a a\"}", Encoding.UTF8);
        http.Context.Request.ContentType = "application/json";
        http.Execute(new TagHandler(tags));
        Assert.IsTrue(tags.ContainsKey("foo"));
        Assert.AreEqual("a a a", tags["foo"].Description);
    }

    [Test] public void GetTagsResponseIsUtf8Encoded() {
        HttpHandlerTest http = HttpHandlerTest.ImitateGet("tags.ashx");
        http.Execute(new TagHandler(tags));
        Assert.IsInstanceOfType(typeof (UTF8Encoding),
                                http.Context.Response.ContentEncoding);
    }

    [Test] public void GetTagsResponseIsApplicationJson() {
        HttpHandlerTest http = HttpHandlerTest.ImitateGet("tags.ashx");
        http.Execute(new TagHandler(tags));
        Assert.AreEqual("application/json",
                        http.Context.Response.ContentType);
    }

    [Test] public void GetNoTags() {
        HttpHandlerTest http = HttpHandlerTest.ImitateGet("tags.ashx");
        http.Execute(new TagHandler(tags));
        Assert.AreEqual("{}", http.Output);
    }

    [Test] public void GetTags() {
        DateTime oneSecondFrom_1970_1_1 = new DateTime(1970, 1, 1, 0, 0, 1, DateTimeKind.Utc);
        HttpHandlerTest http = HttpHandlerTest.ImitateGet("tags.ashx");
        tags["tag"] = new Tag("123", oneSecondFrom_1970_1_1);
        http.Execute(new TagHandler(tags));
        Assert.AreEqual("{\"tag\":{\"description\":\"123\",\"dateTimeCreated\":new Date(1000)}}",
                        http.Output);
    }
}